package com.hongdee.crm.taskConfig.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.Constant;
import com.hongdee.crm.taskConfig.service.ITCrmTaskTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * Created by My on 2017/3/6.
 */
@RestController
@RequestMapping("/crm/taskConfig")
public class TCrmTaskTypeController {
    @Autowired
    private ITCrmTaskTypeService itCrmTaskTypeService;
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody Map data) throws Exception {
        //EntityUtils.valid(data);
        this.itCrmTaskTypeService.saveOrUpdate(data,uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @param start 当前页面
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc    任务类型分配分页
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "taskType",defaultValue = "") String taskType) throws  Exception{
        String oid=TokenUtil.getCompanyId(uid);
        Map map=this.itCrmTaskTypeService.page(start,limit,oid,taskType);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   角色任务类型分配
     * @author 徐钦勇
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        String oid=TokenUtil.getCompanyId(uid);
        List map = this.itCrmTaskTypeService.info(oid,id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   启用start
     * @author 徐钦勇
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmTaskTypeService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   停用stop
     * @author 徐钦勇
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.itCrmTaskTypeService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   角色下拉框
     * @author 徐钦勇
     */
    @RequestMapping("/comboRole")
    @ResponseBody
    public List<Map> comboRole(@RequestParam(name = "uid", defaultValue = "") String uid,
                                              @RequestParam(name = "token", defaultValue = "") String token)throws  Exception{
        String oid=TokenUtil.getCompanyId(uid);
        return this.itCrmTaskTypeService.comboRole(oid);
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   任务类型下拉列表
     * @author 徐钦勇
     */
    @RequestMapping("/comboTask")
    @ResponseBody
    public List<Map> comboTask(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token)throws  Exception{
        String oid=TokenUtil.getCompanyId(uid);
        return this.itCrmTaskTypeService.comboTask(oid);
    }


    /**
     * 
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/comboTaskList")
    @ResponseBody
    public List<Map> comboTaskList(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token)throws  Exception{
        String oid=TokenUtil.getCompanyId(uid);
        return this.itCrmTaskTypeService.comboRoleList(uid);
    }


    @RequestMapping("/tree")
    @ResponseBody
    public ResultEntity tree(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "pCode", defaultValue = "task") String pCode,
                             @RequestParam(name = "type", defaultValue = "") String type,
                             @RequestParam(name = "roleId", defaultValue = "") String roleId) throws Exception {
        ResultEntity res = new ResultEntity();
        List dataList = null;
        if(type.equals(String.valueOf(Constant.TASKPOWER.SEE.ordinal()))){
            dataList=itCrmTaskTypeService.tree("",pCode,"",type);
        }else{
            String oid=TokenUtil.getCompanyId(uid);
            dataList=itCrmTaskTypeService.tree(roleId,pCode,oid,type);
        }
        res.setDataList(dataList);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 获得其已经选中的节点参数
     * @param uid
     * @param token
     * @param type
     * @param roleId
     * @return
     * @throws Exception
     */
    @RequestMapping("/checkedDef")
    @ResponseBody
    public Map checkedDef(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "type", defaultValue = "") String type,
                                   @RequestParam(name = "roleId", defaultValue = "") String roleId) throws Exception {
        Map res = new HashMap();
        String oid=TokenUtil.getCompanyId(uid);
        if(type.equals(String.valueOf(Constant.TASKPOWER.SEE.ordinal()))){
            res.put("check",this.itCrmTaskTypeService.checkedDefSee(oid,roleId));
        }else{
            res.put("check",this.itCrmTaskTypeService.checkedDefCheck(oid,roleId));
        }
        res.put("success",true);
        res.put("msg","获取参数树成功！");
        return res;
    }

    /**
     * 保存所选择的节点信息
     * @param uid
     * @param token
     * @param nodes
     * @param type
     * @param roleId
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveTreeNode")
    @ResponseBody
    public ResultEntity saveTreeNode(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestBody List<String> nodes,
                                     @RequestParam(name = "type", defaultValue = "") String type,
                                     @RequestParam(name = "roleId", defaultValue = "") String roleId) throws Exception {
        String oid=TokenUtil.getCompanyId(uid);
        System.out.println(nodes.toArray());
        itCrmTaskTypeService.saveTaskType(nodes,roleId,oid,type);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("分配任务权限成功！");
        return res;

    }


}
